<template>
  <!-- 角色表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="500px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="110px">
      <el-form-item label="业务领域" prop="businessDomain">
        <el-select
          v-if="!props.isReadonly"
          clearable
          v-model="formData.businessDomain"
          placeholder="请选择"
        >
          <el-option
            v-for="item in businessDomains"
            :key="item.id"
            :label="item.itemLabel"
            :value="Number(item.itemValue)"
          />
        </el-select>
        <span v-else>{{ formData.businessDomainName }}</span>
      </el-form-item>
      <el-form-item label="检测类别编号" prop="checkTypeCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.checkTypeCode"
          placeholder="请输入检测类别编号"
          clearable
        />

        <span v-else>{{ formData.checkTypeCode }}</span>
      </el-form-item>
      <el-form-item label="检测类别名称" prop="checkTypeName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.checkTypeName"
          placeholder="请输入检测类别名称"
          clearable
        />

        <span v-else>{{ formData.checkTypeName }}</span>
      </el-form-item>
      <!-- <el-form-item label="数据版本号" prop="version">
        <el-input-number
          v-if="!props.isReadonly"
          v-model="formData.version"
          placeholder="请输入数据版本号"
          clearable
        />

        <span v-else>{{ formData.version }}</span>
      </el-form-item> -->
      <el-form-item label="备注" prop="remarks">
        <el-input
          type="textarea"
          v-if="!props.isReadonly"
          v-model="formData.remarks"
          placeholder="请输入备注"
          clearable
        />

        <span v-else>{{ formData.remarks }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import { number } from "echarts";
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMCHECKTYPEADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  businessDomain: [
    { required: true, message: "请输入业务领域", trigger: "blur" },
  ],
  checkTypeCode: [
    { required: true, message: "请输入检测类别编号", trigger: "blur" },
  ],
  checkTypeName: [
    { required: true, message: "请输入检测类别名称", trigger: "blur" },
  ],
  version: [{ required: true, message: "请输入数据版本号", trigger: "blur" }],
});

const formRef = ref(ElForm);
let businessDomains = ref([]);
let formData = reactive({
  id: "",
  businessDomain: "",
  businessDomainName: "",
  checkTypeCode: "",
  checkTypeName: "",
  version: "",
  remarks: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        businessDomain,
        businessDomainName,
        checkTypeCode,
        checkTypeName,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        businessDomain,
        businessDomainName,
        checkTypeCode,
        checkTypeName,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
      });
    }
  }
);
async function getBusinessDomain() {
  const data = await request({
    url: "/system/dictionary/getDictionaryItems",
    method: "post",
    data: { dictionaryCode: "BusinessDomain" },
  });
  businessDomains = data.data;
}
/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.businessDomain = "";
  formData.businessDomainName = "";
  formData.checkTypeCode = "";
  formData.checkTypeName = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 检测类别保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mdm/checktype/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

/** 检测类别查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/mdm/checktype/getDetails",
    method: "post",
    data: formData,
  });
  console.log(data);
}

onMounted(() => {
  getBusinessDomain();
  if (formData.id) {
    getDatails();
  }
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
